package vaadin.lab1;

import java.util.ArrayList;
import java.util.List;

import com.vaadin.Application;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Layout;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.Reindeer;

@SuppressWarnings("serial")
public class Lab extends Application {
    private GridLayout content;
    private List<Object> viewIds = new ArrayList<Object>();

    @Override
    public void init() {

        viewIds.add("View 1");
        viewIds.add("View 2");
        viewIds.add("View 3");

        content = new GridLayout(2, 2);

        Window mainWindow = new Window("Lab", content);
        setMainWindow(mainWindow);

        content.setSizeFull();
        content.setColumnExpandRatio(1, 1.0f);
        content.setRowExpandRatio(1, 1.0f);

        buildHeader();

        buildLeftNavigation();

        setCurrentView("View 1");
    }

    private void buildHeader() {
        HorizontalLayout header = new HorizontalLayout();
        header.setSpacing(true);
        content.addComponent(header, 0, 0, 1, 0);
        content.setComponentAlignment(header, Alignment.TOP_RIGHT);
        addButtons(header);
    }

    private Layout createView(String content) {
        final Label lbl = new Label(content);
        lbl.setSizeUndefined();
        lbl.setStyleName(Reindeer.LABEL_H1);
        VerticalLayout layout = new VerticalLayout();
        layout.setStyleName(Reindeer.LAYOUT_BLACK);
        layout.addComponent(lbl);
        layout.setComponentAlignment(lbl, Alignment.MIDDLE_CENTER);
        return layout;
    }

    private void setCurrentView(Object viewId) {
        Component component = null;
        component = createView(viewId.toString());
        component.setSizeFull();
        final Component oldComponent = content.getComponent(1, 1);
        if (oldComponent == null) {
            content.addComponent(component, 1, 1);
            content.setComponentAlignment(component, Alignment.MIDDLE_CENTER);
        } else {
            content.replaceComponent(oldComponent, component);
        }
    }

    private void buildLeftNavigation() {
        VerticalLayout layout = new VerticalLayout();
        layout.setSizeUndefined();
        layout.setSpacing(true);
        content.addComponent(layout, 0, 1);
        addButtons(layout);
    }

    private void addButtons(ComponentContainer componentContainer) {
        for (final Object viewId : viewIds) {
            Button naviButton = new Button(viewId.toString());
            naviButton.addListener(new Button.ClickListener() {
                @Override
                public void buttonClick(ClickEvent event) {
                    setCurrentView(viewId);
                }
            });
            naviButton.setWidth("150px");
            componentContainer.addComponent(naviButton);
        }
    }
}